﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Win32;
using WPF_DbCenter.CommonMethod;
using System.Security;
using System.IO;
using System.Data;
using XLCS.Common;
using System.Data.SqlServerCe;

namespace WPF_DbCenter.AdditionUI
{
    /// <summary>
    /// Interaction logic for Excel2SDF.xaml
    /// </summary>
    public partial class Excel2SDF : BaseUI.BaseFadeDialog
    {
        public Excel2SDF()
        {
            InitializeComponent();
            this.txtSrcFile.Drop += new DragEventHandler(textBox1_Drop);
        }

        void textBox1_Drop(object sender, DragEventArgs e)
        {
            e.Handled = true;
            if (e.Data.GetDataPresent(DataFormats.FileDrop, true))
            {
                string[] fileNames = e.Data.GetData(DataFormats.FileDrop, true) as string[];
                if (fileNames.Length == 1)
                {
                    // Check for a file (a directory will return false).
                    if (File.Exists(fileNames[0]))
                    {
                        // At this point we know there is a single file.
                        this.txtSrcFile.Text = fileNames[0];
                    }
                }

            }
        }

        public void Select(object sender, RoutedEventArgs e)
        {
            OpenFileDialog sf = new OpenFileDialog();
            sf.Filter = AppEntry.EXCEL_FILE_FILTER;
            if (sf.ShowDialog() == true)
            {
                this.txtSrcFile.Text = sf.FileName;
            }
        }


        public void Convert(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(txtSrcFile.Text))
            {
                if (!(bool)checkBox2.IsChecked)
                {
                    MessageBox.Show("Please select source access file");
                    return;
                }
            }
            if (string.IsNullOrEmpty(txtTargetFile.Text))
            {
                MessageBox.Show("Please select target file");
                return;
            }

            string excelFile = txtSrcFile.Text;
            
            string sdfFile = txtTargetFile.Text;
            try
            {
                if((bool)checkBox2.IsChecked)
                {
                    foreach (string item in Directory.GetFiles(excelFile))
                    {
                        if (CommonUtil.ConvertEXCELToSdf(item, sdfFile, (bool)checkBox1.IsChecked, txtPwd.Password))
                        {
                            MessageBox.Show(String.Format("Sync data from \r\n{0} \r\nto \r\n{1} \r\nSuccessful.", item, sdfFile));
                        }
                    }

                    Close();
                }
                else
                {
                if (CommonUtil.ConvertEXCELToSdf(excelFile, sdfFile, (bool)checkBox1.IsChecked, txtPwd.Password))
                {
                    MessageBox.Show(String.Format("Sync data from \r\n{0} \r\nto \r\n{1} \r\nSuccessful.", excelFile, sdfFile));
                    Close();
                }
                }
            }
            catch (Exception ee)
            {
                XLCS.Common.ProcessException.DisplayErrors(ee);
            }
        }

        /// <summary>
        /// Select Target Sql Ce Db
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog sf = new OpenFileDialog();
            sf.Filter = AppEntry.SQLCE_FILE_FILTER;
            if (sf.ShowDialog() == true)
            {
                this.txtTargetFile.Text = sf.FileName;
            }
        }

  
    }
}

